home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 12078 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.2 KB

  1. Path: lrz-muenchen.de!news
  2. From: watzka@stat.uni-muenchen.de (Kurt Watzka)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Storing C Functions In An Array?
  5. Date: 28 Mar 1996 18:55:12 GMT
  6. Organization: Leibniz-Rechenzentrum, Muenchen (Germany)
  7. Distribution: world
  8. Message-ID: <4jenag$394@sparcserver.lrz-muenchen.de>
  9. References: <4jc1u7$5e9@infa.central.susx.ac.uk>
  10. NNTP-Posting-Host: sun2.lrz-muenchen.de
  11.  
  12. tepd6@central.susx.ac.uk (George Rattray) writes:
  13.  
  14. >Can someone help me, I'm trying to store functions in an array at
  15. >compile time. The following functions are defined as
  16.  
  17. >    struct complex func(struct complex, CHNL);
  18.  
  19. >I want to store them in an array ch[N];
  20.  
  21. >Therefor using the function with an index as 
  22.  
  23. >    ret = ch[0](x,chnl);
  24.  
  25. >Any help will be most appreciated, Thanks
  26.  
  27. This can be done using function pointers. Since a function "name"
  28. mentioned in an expression decays to a pointer to that function,
  29. and since you can easily call a function through a pointer, there
  30. is no need to declare an "array of functions".
  31.  
  32.      typedef struct compley (* func)(struct compley, CHNL);
  33.  
  34.      func ch[N];
  35.  
  36.      ret = ch[0](x, chnl);
  37.  
  38. Kurt
  39. --
  40. | Kurt Watzka                             Phone : +49-89-2180-6254
  41. | watzka@stat.uni-muenchen.de
  42.  
  43.